package n11.second;
//测试代码

import java.util.HashMap;
import java.util.Map;

public class Test {
	private static Map<Integer, Area>map;
	private static final RuntimeException ID_NULL=new RuntimeException("身份证号不能是null~");
	private static final RuntimeException AREA_ERROR=new RuntimeException("身份证地区编码不正确~");
	static {
		map=new HashMap<Integer, Area>();
		String[] hs = Utils.getAreaString().split("\r\n");
		for(String h: hs) {
			String[]i=h.split("\t");//item
			int id=Integer.parseInt(i[0]);
			int pid=Integer.parseInt(i[1]);
			int bm=Integer.parseInt(i[3]);
			Area area = new Area(id, pid, i[2], bm);
			map.put(bm, area);
			if(id<800)map.put(id, area);
		}
	}
	/**
	 * (2)实现getAddress()方法的功能，可以根据身份证号获取其地址信息
	 * @param idNumber 身份证号，这里为简化不考虑长度，但至少提供6位数字地区编码
	 * @return 返回地址信息，若(1)参数是null，则抛出ID_NULL异常，若(2)参数长度不足6位 (3)地区编码包含非数字 (4)地区编码不存在，则抛出AREA_ERROR异常
	 */
	public static String getAddress(String idNumber) {
		if(idNumber==null)throw ID_NULL;
		try {
			int id, bm = Integer.parseInt(idNumber.substring(0, 6));
			Area a = map.get(bm);
			if(a==null)throw AREA_ERROR;
			String address=a.getArea();
			while((id=a.getPid())!=0) {
				a = map.get(id);
				address=a.getArea()+address;
			}
			return address;
		}catch(Exception e) {
			throw AREA_ERROR;
		}
	}
	public static void main(String[] args) {
		try {
			String address = getAddress("41010520040618287X");
			System.out.println(address);
		}catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
